Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Namespace BMLERP
	Friend Class frmINV120
		Inherits System.Windows.Forms.Form
		Dim OldLMLOT As String
		Sub ButtonStatus()
			cmdAddnew.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING	' And txtIMITN <> ""
			cmdDelete.Enabled = Not ISEDITING 'And txtIMITN <> ""
			cmdCancel.Enabled = ISEDITING
			'txtIMITN.Locked = Not ISADDING ' Lock item code tru truong hop ISADDING=true
			'txtDepartment.Enabled = ISEDITING
			'cboDepartment.Visible = Not ISADDING
			'txtDepartmentID.Visible = ISADDING
		End Sub

		Private Sub Control(ByRef GT As Object)

			txtLMITN.Enabled = GT
			cboLMITN.Enabled = GT
			txtLMLOT.Enabled = GT
			txtLMPDT.Enabled = GT
			txtLMEDT.Enabled = GT
			'  txtLMRID.Enabled = GT
			'  txtLMLDT.Enabled = GT
			'   txtLMSTT.Enabled = GT

			txtLMITN.ReadOnly = Not GT
			cboLMITN.Locked = Not GT
			txtLMLOT.ReadOnly = Not GT
			txtLMPDT.ReadOnly = Not GT
			txtLMEDT.ReadOnly = Not GT
			'  txtLMRID.Locked = Not GT
			'  txtLMLDT.Locked = Not GT
			'  txtLMSTT.Locked = Not GT

			cboList.Enabled = Not GT
			cboList.Locked = GT
		End Sub
		Private Sub WriteToScreen(ByRef tri As Byte)
			Dim TBL As ADODB.Recordset
			If tri = 0 Then	'gan rong
				txtLMITN.Text = ""
				cboLMITN.BoundText = ""
				txtLMLOT.Text = ""
				txtLMPDT.Text = CStr(Today)
				txtLMEDT.Text = CStr(Today)
				'txtLMRID = "LM"
				'txtLMLDT = Date
				'txtLMSTT = 0
			Else
				TBL = New ADODB.Recordset
				'TBL.Open "  Select ILM.*From ILM where  LMITN ='" & cboList.BoundText & "' and LMLOT = '" & cboList.Tag & "'", DBconn, adOpenKeyset, adLockOptimistic, adCmdText
				TBL.Open("  Select ILM.* From ILM where  LMLOT ='" & cboList.BoundText & "'", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)
				If TBL.EOF = False Then
					txtLMITN.Text = IIf(IsDBNull(TBL.Fields("lmitn").Value), "", TBL.Fields("lmitn").Value)
					cboLMITN.BoundText = IIf(IsDBNull(TBL.Fields("lmitn").Value), "", TBL.Fields("lmitn").Value)
					txtLMLOT.Text = IIf(IsDBNull(TBL.Fields("lmlot").Value), "", TBL.Fields("lmlot").Value)
					txtLMPDT.Text = IIf(IsDBNull(TBL.Fields("lmPDT").Value), Today, TBL.Fields("lmPDT").Value)
					txtLMEDT.Text = IIf(IsDBNull(TBL.Fields("lmEDT").Value), Today, TBL.Fields("lmEDT").Value)
					'  txtLMRID = "LM"
					' txtLMLDT = Date
					'txtLMSTT = 0
				Else
					txtLMITN.Text = ""
					cboLMITN.BoundText = ""
					txtLMLOT.Text = ""
					txtLMPDT.Text = CStr(Today)
					txtLMEDT.Text = CStr(Today)
					'txtLMRID = "LM"
					'txtLMLDT = Date
					'txtLMSTT = 0
				End If
				TBL.Close()
			End If
			OldLMLOT = txtLMLOT.Text
		End Sub
		Private Sub WriteToFile(ByRef tri As Byte)
			Dim cmdExecute As Object
			Dim SQLInsert As String
			If tri = 0 Then
				SQLInsert = " INSERT INTO ILM " & "(LMITN,LMLOT,LMPDT,LMEDT,LMSTT,LMRID,LMLDT,LMRCU,LMISU,LMAJU,LMRCW" & ",LMISW,LMAJW) " & " VALUES " & "('" & VB.Left(txtLMITN.Text, 15) & "', '" & VB.Left(txtLMLOT.Text, 10) & "' ,'" & VB6.Format(CDate(txtLMPDT.Text), "yyyy-mm-dd hh:mm:ss") & "', '" & VB6.Format(CDate(txtLMEDT.Text), "yyyy-mm-dd hh:mm:ss") & "', 0, 'LM'" & ", '" & VB6.Format(Today, "yyyy-mm-dd hh:mm:ss") & "',0,0,0,0,0,0)"
			Else
				SQLInsert = "UPDATE ILM SET LMITN = '" & VB.Left(txtLMITN.Text, 15) & "', LMLOT ='" & VB.Left(txtLMLOT.Text, 10) & "'" & ", LMPDT ='" & VB6.Format(CDate(txtLMPDT.Text), "yyyy-mm-dd hh:mm:ss") & "'" & ", LMEDT = '" & VB6.Format(CDate(txtLMEDT.Text), "yyyy-mm-dd hh:mm:ss") & "'" & " WHERE LMLOT ='" & OldLMLOT & "'"
			End If

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = SQLInsert
			cmdExecute.Execute()

		End Sub



		Private Sub cboList_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboList.ClickEvent
			If eventArgs.area = 2 Then
				WriteToScreen((1))
			End If
		End Sub

		Private Sub cboLMITN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboLMITN.ClickEvent
			If eventArgs.area = 2 Then
				txtLMITN.Text = cboLMITN.BoundText
				With Adodc2.Recordset
					.MoveFirst()
					Do Until .Fields("imitn").Value = cboLMITN.BoundText
						.MoveNext()
						If .EOF Then Exit Do
					Loop
					If .EOF = False Then
						If .Fields("IMLOT").Value = True Then
							txtLMLOT.Enabled = True
							txtLMLOT.ReadOnly = False
						Else
							MsgBox("This Item haven't got lot.")
							txtLMLOT.Text = ""
							txtLMLOT.Enabled = False
							txtLMLOT.ReadOnly = True
						End If
					End If
				End With

			End If
		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			ISADDING = True
			ISEDITING = True
			WriteToScreen((0))
			Control((True))
			' Ham Gan tri man hinh
			ButtonStatus()
			txtLMITN.Focus()

		End Sub
		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			' Gan tri man nhinh
			ISADDING = False
			ISEDITING = False
			Control((False))
			ButtonStatus()
			txtLMITN.Text = cboList.BoundText
			WriteToScreen((1))

		End Sub

		Private Sub cmdDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDelete.Click
			Dim cmdExecute As Object
			ISADDING = False
			ISEDITING = False
			' Ham delete theo Item chon
			Dim sql As String
			sql = "DELETE FROM ILM WHERE LMITN = '" & txtLMITN.Text & "' and LMLOT = '" & txtLMLOT.Text & "'"

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = sql
			cmdExecute.Execute()
			Adodc1.Refresh()
			WriteToScreen((0))
			ButtonStatus()

		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			' Ham gan tri man hinh
			ISADDING = False
			ISEDITING = True
			Control((True))
			txtLMITN.Focus()
			ButtonStatus()

		End Sub

		Private Sub cmdexit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			If ISADDING = False Then
				WriteToFile((1))
			Else
				WriteToFile((0))
			End If
			Adodc1.Refresh()
			Control((False))
			'''''''''''
			ISADDING = False
			ISEDITING = False

			ButtonStatus()

		End Sub

		Private Sub frmINV120_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			If Not InitSuccess Then
				BIENCHUNG()
			End If

			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc1.RecordSource = "ILM"	'lote master
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc2.RecordSource = "IIM"	'company profile
			Adodc2.Refresh()
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			Me.VB6_AddADODataBinding()
			Me.cboList.RowSource = Me.ADOBind_Adodc1.DataSource
			Me.cboLMITN.RowSource = Me.ADOBind_Adodc2.DataSource

		End Sub

		Private Sub txtLMITN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLMITN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				With Adodc2.Recordset
					.MoveFirst()
					Do Until .Fields("imitn").Value = txtLMITN.Text
						.MoveNext()
						If .EOF Then Exit Do
					Loop
					If .EOF = False Then
						cboLMITN.BoundText = txtLMITN.Text
						cboLMITN_ClickEvent(cboLMITN, New AxMSDataListLib.DDataComboEvents_ClickEvent((2)))
					Else
						cboLMITN.BoundText = ""
						MsgBox("Khong co Item")
						cboLMITN.Focus()
					End If
				End With
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub



		Private Sub txtLMLOT_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLMLOT.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If ValidMaso(OldLMLOT, txtLMLOT, "ILM", " LMLOT='" & txtLMLOT.Text & "'") Then
					MsgBox("This code is already exist")
					txtLMLOT.Focus()
				Else
					txtLMPDT.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace